package org.webrtc.audio;

import android.media.AudioDeviceInfo;
import android.util.Log;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author jinyf
 */
public class MyAudioRecordSource {

    private int audioSource;
    private int sampleRate;
    private int channelConfig;
    private int audioFormat;
    private int bufferSizeInBytes;

    public MyAudioRecordSource(int audioSource, int sampleRate, int channelConfig, int audioFormat, int bufferSizeInBytes) {
        this.audioSource = audioSource;
        this.bufferSizeInBytes = bufferSizeInBytes;
        this.sampleRate = sampleRate;
        this.channelConfig = channelConfig;
        this.audioFormat = audioFormat;

    }

    private BlockingDeque<byte[]> mPcm = new LinkedBlockingDeque<>();

    public int getChannelCount() {
        return 1;
    }

    public int getSampleRate() {
        return sampleRate;
    }

    public int getAudioFormat() {
        return audioFormat;
    }


    public byte[] read() {
//        try {
//            return mPcm.take();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return mPcm.poll();
    }

    public void release() {
        mPcm.clear();
    }

    public void stop() {
        mPcm.clear();
    }

    public boolean setPreferredDevice(AudioDeviceInfo preferredDevice) {
        return false;
    }

    public void addData(byte[] pcm) {
        if (mPcm.size()>500){
            Log.i("223223","return");
            return;
        }
        Log.i("223223","add");
        mPcm.add(pcm);
    }
}
